package com.yfl.tingyu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yfl.tingyu.mapper.HostMapper;
import com.yfl.tingyu.pojo.Host;
import com.yfl.tingyu.service.IHostService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author ${author}
 * @since 2020-05-29
 */
@Service
public class HostServiceImpl extends ServiceImpl<HostMapper, Host> implements IHostService {

    @Override
    public Map<String, Object> selectHosts(Map<String, Object> params) {
        Integer page =Integer.valueOf((String) params.get("page"));
        Integer rows = Integer.valueOf((String) params.get("rows"));

        System.out.println(page);
        //创建分页对象
        Page<Map<String, Object>> hostPage = new Page<>(page,rows);

        //创建条件查询对象
        QueryWrapper<Map<String,Object>> qw=new QueryWrapper<>();

        //条件查询
        //姓名模糊查询
        if (StringUtils.isNotBlank((String) params.get("hname"))){
            qw.like("hname",params.get("hname"));
        }
        //用户状态查询
        if (StringUtils.isNotBlank((String) params.get("status"))){
            qw.eq("status",params.get("status"));
        }
        //折扣
        if (StringUtils.isNotBlank((String) params.get("hpdiscount"))){
            qw.eq("hpdiscount",params.get("hpdiscount"));
        }
        //星推荐
        if (StringUtils.isNotBlank((String) params.get("hpstar"))){
            if("1".equals((String) params.get("hpstar"))){
                qw.eq("hpstar",params.get("hpstar"));
            }else {
                qw.isNull("hpstar");
            }
        }

        //权重排序
        if (StringUtils.isNotBlank((String) params.get("strong"))){
           if ("desc".equals((String) params.get("strong"))){
                    qw.orderByDesc("strong");
           }else {
               qw.orderByAsc("strong");
           }
        }
        //订单量排序
        if (StringUtils.isNotBlank((String) params.get("ordernumber"))){
            if ("desc".equals((String) params.get("ordernumber"))){
                qw.orderByDesc("ordernumber");
            }else {
                qw.orderByAsc("ordernumber");
            }
        }

        IPage<Map<String, Object>> mapIPage = baseMapper.selectHosts(hostPage, qw);

        //创建datagrid 需要的数据

        Map<String,Object> datagridDate=new HashMap<>();
        //total
        datagridDate.put("total",mapIPage.getTotal());
        //rows
        datagridDate.put("rows",mapIPage.getRecords());

        return datagridDate;
    }
}
